unit ULookupEditorFrame;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs,  StdCtrls, DBCtrls, DB, IBCustomDataSet, IBTable,
  UDatabase, UEditorFrame, UField, IBQuery, Grids, DBGrids, dbLookup, ULog;

type
  TLookupEditFrame = class(TEditorFrame)
    ds: TDataSource;
    iq: TIBQuery;
    cbValue: TDBLookupComboBox;
    procedure iqAfterTransactionEnd(Sender: TObject);
    procedure cbValueClick(Sender: TObject);
  private
    FField: TSchLookupField;
    procedure SetField(Value: TSchLookupField);
  protected
    function GetData: string; override;
    procedure SetData(const Value: string); override;
  public
    procedure UpdateField;
    property Field: TSchLookupField read FField write SetField;
    constructor Create(AOwner: TComponent); override;
  end;

implementation

{$R *.dfm}

{ TLookupEditorFrame }

procedure TLookupEditFrame.cbValueClick(Sender: TObject);
begin
  inherited;
  Empty := False;
end;

constructor TLookupEditFrame.Create(AOwner: TComponent);
begin
  inherited;
  Empty := True;
end;

function TLookupEditFrame.GetData: string;
begin
  Result := iq.FieldValues[FField.TableDef.RefKey];
end;

procedure TLookupEditFrame.iqAfterTransactionEnd(Sender: TObject);
begin
  inherited;
  iq.Open;
end;

procedure TLookupEditFrame.SetData(const Value: string);
begin
  cbValue.KeyValue := Value;
  Empty := False;
end;

procedure TLookupEditFrame.SetField(Value: TSchLookupField);
begin
  if Value = FField then
    Exit;
  FField := Value;
  UpdateField;
end;

procedure TLookupEditFrame.UpdateField;
begin
  iq.SQL.Text := Format(CSelectAll, [FField.TableDef.RefTable]);
  cbValue.KeyField := FField.TableDef.RefKey;
  cbValue.ListField := FField.Name;
  iq.Open;
  iq.FetchAll;
end;

end.
